import fetch from 'common/src/utils/fetch';
import UTILS from "common/src/utils/utils";

// 免修免听开关设置
export function exSwitch(data) {
  return fetch({
    url: '/api/cultureservice/exempt/switch',
    method: 'post',
    data
  })
}

//免修免听申请列表
export function exApplyList(data){
  return fetch ({
    url:'api/cultureservice/exempt/applyList',
    method:'post',
    data
  })
}

//免修免听文件上传
export function exUpload(data){
  return fetch ({
    url:'api/cultureservice/exempt/objectKey',
    method:'post',
    data
  })
}
//免修免听文件删除
export function exDeleteFile(data){
  return fetch ({
    url:'api/cultureservice/exempt/delete',
    method:'DELETE',
    data
  })
}
//查询免修/免听申请表下载路径
export function selectObjectKey(data){
  return fetch ({
    url:'api/cultureservice/exempt/selectObjectKey?templateType='+data.templateType,
    method:'get'
  })

}
 //查询学生未修读且在培养计划中的课程
export function getCourses(params){
  return fetch ({
    url:`api/cultureservice/exempt/course?${getParams(params)}`,
    method:'get'
  })
}

//免修免听申请确认
export function apply(data){
  return fetch ({
    url:'api/cultureservice/exempt/apply',
    method:'post',
    data
  })
}
//免修免听审核列表查询
export function exApprovedList(data){
  return fetch ({
    url:'api/cultureservice/exempt/approvedList',
    method:'post',
    data
  })
}
//免修免听审批进度查询
export function exHistory(no){
  return fetch ({
    url:'api/cultureservice/exempt/history?workflowNo='+no,
    method:'get'
  })
}
//查询免修免听模板
export function queryTemplate(){
  return fetch ({
    url:'api/cultureservice/exempt/template',
    method:'post'
  })
}

//撤回免修免听申请记录（学生专用）
export function withdraw(workflowNo){
  return fetch ({
    url:'api/cultureservice/exempt/withdraw?workflowNo='+workflowNo,
    method:'get'
  })
}

function getParams(data = {}) {
    let keys = Object.keys(data).filter(item => data[item]);
    let params = keys.reduce((prev, curr, index) => {
        prev += curr + '=' + data[curr];
        if (index < keys.length - 1) {
            prev += '&';
        }
        return prev;
    }, '');
    return params;
}

 
 export function getSchedule(params){
  return fetch ({
    url:`api/cultureservice/exempt/schedule?${getParams(params)}`,
    method:'get'
  })
}

//撤回
export function recall(data){
  return fetch ({
    url: '/api/workflow/approval/recall',
    method: 'post',
    data: data
  })
}


//撤回成功后调用
export function editStudyType(data){
  return fetch ({
    url: '/api/cultureservice/exempt/editStudyType',
    method: 'post',
    data: data
  })
}

//免修免听审核列表查询
export function queryApplyInfoById(params) {
    return fetch({
        url: 'api/cultureservice/exempt/queryApplyInfoById',
        method: 'get',
        params
    })
}


export default {
    apply,
    withdraw,
    exSwitch,
    exUpload,
    exHistory,
    getCourses,
    exApplyList,
    exDeleteFile,
    queryTemplate,
    exApprovedList,
    selectObjectKey,
    getSchedule,
    recall,
    editStudyType
}